package com.example.aiyou.passwordmanage.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.example.aiyou.passwordmanage.R;
import com.example.aiyou.passwordmanage.base.ActivityManage;
import com.example.aiyou.passwordmanage.base.BaseActivity;
import com.example.aiyou.passwordmanage.pojo.Password;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AddActivity extends BaseActivity {
    private MainActivity mainActivity;
    private EditText editName;
    private EditText editAccount;
    private EditText editPhone;
    private EditText editPassword;
    private EditText editDescription;
    private EditText editNick;
    private AutoCompleteTextView editEmail;
    private Spinner editType;
    private int type;
    private long id = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        final Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        editName = (EditText) findViewById(R.id.edit_name);
        editAccount = (EditText) findViewById(R.id.edit_account);
        editPhone = (EditText) findViewById(R.id.edit_phone);
        editPassword = (EditText) findViewById(R.id.edit_password);
        editDescription = (EditText) findViewById(R.id.edit_description);
        editNick = (EditText) findViewById(R.id.edit_nick);
        editType = (Spinner)findViewById(R.id.edit_type);
        editEmail = (AutoCompleteTextView)findViewById(R.id.edit_email);
        showInfo();
        editType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                type = i;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                type = 0;
            }
        });
        if(bundle.getInt("mark") == 0){
            BaseActivity.setId(0);
        }else if(bundle.getInt("mark") == 1){
            setTitle("编辑");
            Password password = (Password) bundle.getSerializable("password");
            BaseActivity.setId(password.getId());
            editName.setText(password.getName());
            editAccount.setText(password.getAccount());
            editDescription.setText(password.getDescription());
            editPhone.setText(password.getPhone());
            editPassword.setText(password.getPassword());
            editNick.setText (password.getNick());
            editType.setSelection(password.getType());
            editEmail.setText (password.getEmail());
            type = password.getType();
            id = password.getId();
        }
        Button buttonAdd = (Button) findViewById(R.id.button_save);
        buttonAdd.setOnClickListener(
                new View.OnClickListener(){
                    @Override
                    public void onClick(View view){
                        attemptAdd();
                    }
                }
        );
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.menu_add,menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem){
        int id = menuItem.getItemId();
        if(id == R.id.action_about){
            Intent intent = new Intent(AddActivity.this,AboutActivity.class);
            startActivity(intent);
            finish();
            return true;
        }else if(id == R.id.action_home){
            Intent intent = new Intent(AddActivity.this,MainActivity.class);
            startActivity(intent);
        }else if(id == R.id.action_exit){
            ActivityManage.finishAll();
            finish();
            return true;
        }
        finish();
        return super.onOptionsItemSelected(menuItem);
    }
    public static void actionStart(Context context,int mark){
        Intent intent = new Intent(context,AddActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("mark",mark);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }
    public void saveItem(){
        String icon;
        if(type == 1){
            icon = "weixin";
        }else if(type == 2){
            icon = "qq";
        }else if(type == 4){
            icon = "taobao";
        }else if(type == 5){
            icon = "weibo";
        }else if(type == 7){
            icon = "creditcard";
        }else if(type == 8){
            icon = "tongxunshang";
        }else if(type == 9){
            icon = "vip";
        }else if (type == 3){
            icon = "alipay";
        }else if (type == 6){
            icon = "github";
        } else{
            icon = "key";
        }
        Password password = new Password(editName.getText().toString(),editPhone.getText().toString(),editAccount.getText().toString(),editDescription.getText().toString(),editPassword.getText().toString(),type,icon, new Date(),editEmail.getText().toString(),editNick.getText().toString());
        if(0 != id){
            password.update(id);
            return;
        }
        password.save();
    }
    private void attemptAdd() {
        Boolean cancel = true;
        View focusView = null;
        String name = editName.getText().toString();
        String password = editPassword.getText().toString();
        String account = editAccount.getText().toString();
        String email = editEmail.getText().toString();
        if(TextUtils.isEmpty(name)){
            editName.setError("名称不能为空");
            focusView = editName;
            cancel = false;
        }
        if(TextUtils.isEmpty(account)){
            editAccount.setError("账号不能为空");
            focusView = editAccount;
            cancel = false;
        }
        if(TextUtils.isEmpty(password)){
            editPassword.setError("密码不能为空");
            focusView = editPassword;
            cancel = false;
        }
        if (!TextUtils.isEmpty(email) && !isEmailValid(email)){
            focusView = editEmail;
            editEmail.setError("邮箱不可用");
            cancel = false;
        }
        if(cancel){
            saveItem();
            mainActivity.actionStart(AddActivity.this);
            finish();
        }else{
            focusView.requestFocus();
        }
    }
    private boolean isEmailValid(String email) {
        return email.contains("@");
    }
    public void showInfo(){
        SharedPreferences sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
        String phone = sharedPreferences.getString("phone","");
        String email = sharedPreferences.getString("email","");
        String account = sharedPreferences.getString("account","");
        String nick = sharedPreferences.getString("nick","");
        if(!TextUtils.isEmpty(phone)){
            editPhone.setText(phone.trim());
        }
        if(!TextUtils.isEmpty(email)){
            editEmail.setText(email.trim());
        }
        if(!TextUtils.isEmpty(account)){
            editAccount.setText(account.trim());
        }
        if(!TextUtils.isEmpty(nick)){
            editNick.setText(nick.trim());
        }
    }
}
